# 删除条数少于等于10的asin记录
import traceback
# import pymysql
# db = pymysql.connect(host='localhost', port=3306,
#                          user='root', passwd='', db='lj', charset='utf8mb4')
# cursor = db.cursor()
# sql = "SELECT asin,COUNT(page)<=30 FROM `amazon_reviews` GROUP BY asin "
# cursor.execute(sql)
# art_urlList = []
# results = cursor.fetchall()
# print(results)
# asin_List = []
# for i in results:
#     if i[1] == 1:
#         asin_List.append(i[0])
# print(asin_List)
# for asin in asin_List:
#     sql = "DELETE FROM `amazon_reviews` WHERE asin = '{}'".format(asin)
#     print(sql)
#     row_count = cursor.execute(sql)
#     # 在执行增删改操作时，需要向数据库提交操作，否则操作不成功
#     db.commit()
# print(len(asin_List))
# db.close()

# #查询数据库中的asin
# import traceback
# import pymysql
# db = pymysql.connect(host='localhost', port=3306,
#                          user='root', passwd='', db='lj', charset='utf8mb4')
# cursor = db.cursor()
# sql = "SELECT asin FROM `amazon_reviews` GROUP BY asin "
# cursor.execute(sql)
# art_urlList = []
# results = cursor.fetchall()
# print(results)
# asin_List = []
# for i in results:
#     asin_List.append(i[0])
# print(asin_List)
# print(len(asin_List))
# db.close()
# from retrying import retry
#
#
# def run2(exception):
#     print('11')
#     return isinstance(exception, ZeroDivisionError)
#
#
# def _result(result):
#     return result is None
#
#
# @retry(stop_max_attempt_number=6, retry_on_result=_result)
# def run():
#     print("开始重试")
#     a = 1 / 0
#
#
# if __name__ == '__main__':
#     run()
# print(A.content_list)
# db = pymysql.connect(host='localhost', port=3306,
#                      user='root', passwd='', db='lj', charset='utf8mb4')
# cursor = db.cursor()
# datalist = (tuple(x) for x in A.content_list)
# sql = "INSERT INTO amazon_reviews(review_id, \
#        profile_name, review_date, review_body, page, asin) \
#        VALUES (%s,%s,%s,%s,%s,%s)"
# try:
#     # 执行sql语句
#     cursor.executemany(sql, datalist)
#     # 提交到数据库执行
#     db.commit()
# except:
#     traceback.print_exc()
#     # 如果发生错误则回滚
#     db.rollback()
# # 关闭数据库连接
# db.close()

# from multiprocessing.dummy import Pool as Threadpool
# import random
# import threading
# class A:
#
#     def __init__(self):
#         self.all = 0
#         self.c = 0
#
#     def crawler(self, asin):
#         a = random.randint(1, 3)
#         # self.getC(a)
#         th = Threadpool(2)
#         th.map(self.getC, [a, a])
#         print(str(a) + ':' + asin + ' : ' + str(self.c))
#
#     def getC(self, a):
#         for i in range(a):
#             self.c += 1
#             self.all += 1
#
#
# A = A()
#
# th = Threadpool(3)
# th.map(A.crawler, ['A', 'B', 'C'])
# print('all : ' + str(A.all))

# import traceback
# import logging
# logging.basicConfig(format='%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s',
#                     level=logging.INFO,
#                     filename='a.log',
#                     filemode='a')
# def a():
#     try:
#         logging.info('aaa')
#         1/0
#     except:
#         err = traceback.format_exc()
#         logging.info(err)
#         logging.info('bbb')
# a()
import requests
import threading
import random

headers = {
    "authority": "www.amazon.com",
    "method": "GET",
    "scheme": "https",
    "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,"
              "application/signed-exchange;v=b3;q=0.9",
    "accept-encoding": "gzip, deflate, br",
    "accept-language": "zh-CN,zh;q=0.9,zh-TW;q=0.8,en-US;q=0.7,en;q=0.6",
    "sec-fetch-dest": "document",
    "sec-fetch-mode": "navigate",
    "sec-fetch-site": "same-origin",
    'connection': 'close',
    "sec-fetch-user": "?1",
    "upgrade-insecure-requests": "1",
}
downlink_list = ['1.75', '10', '2.1', '2.25', '2.7', '2.2', '1.8', '2.15']


class MyThread(threading.Thread):

    def __init__(self, func, args=()):
        super(MyThread, self).__init__()
        self.func = func
        self.args = args

    def run(self):
        self.result = self.func(*self.args)

    def get_result(self):
        try:
            return self.result  # 如果子线程不使用join方法，此处可能会报没有self.result的错误
        except Exception:
            return None


def post(asin):
    url = "https://fls-na.amazon.com/1/batch/1/OE/"
    payload = "{\"rid\":\"VB4KEYSJEX31S6YCGTQM\",\"sid\":\"139-5890879-8867853\",\"mid\":\"ATVPDKIKX0DER\",\"sn\":\"www.amazon.com\",\"reqs\":[{\"cel\":{\"k\":\"mmm1\",\"e\":[{\"x\":1301,\"y\":191,\"t\":291508},{\"x\":1299,\"y\":193,\"t\":291585}],\"min\":0,\"max\":104,\"avg\":37}},{\"cel\":{\"k\":\"mmm1\",\"e\":[{\"x\":1293,\"y\":193,\"t\":291609},{\"x\":1196,\"y\":185,\"t\":291753},{\"x\":1164,\"y\":179,\"t\":291849},{\"x\":1123,\"y\":134,\"t\":292682},{\"x\":884,\"y\":333,\"t\":292804}],\"min\":0,\"max\":104,\"avg\":36}},{\"cel\":{\"k\":\"hrt\",\"t\":297618}},{\"csm\":{\"k\":\"chk\",\"f\":15,\"l\":{\"csmcount\":107,\"cel\":70,\"navigation\":2,\"csm\":15},\"s\":\"full\"}},{\"csmcount\":{\"counter\":\"foresterPayloadSize\",\"t\":0,\"value\":615}}]}"
    headers = {
        'authority': 'images-na.ssl-images-amazon.com',
        'cache-control': 'max-age=0',
        'upgrade-insecure-requests': '1',
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36',
        'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
        'sec-fetch-site': 'none',
        'sec-fetch-mode': 'navigate',
        'sec-fetch-user': '?1',
        'sec-fetch-dest': 'document',
        'accept-language': 'zh-CN,zh;q=0.9,zh-TW;q=0.8,en-US;q=0.7,en;q=0.6',
        'if-modified-since': 'Wed, 08 Feb 2012 17:16:51 GMT',
        'Content-Type': 'application/json',
        'Cookie': 'session-id=146-1686273-6519821; session-id-time=2082787201l; i18n-prefs=USD; ubid-main=135-2458626-8322753; x-main="DwJkA46ogoyM6vvaFxkBGDlZjfJiJnv7ZjKRF0bG9qR8sM6ApkusXAex8i?3GbvA"; lc-main=en_US; sp-cdn="L5Z9:CN"; session-token=yms/VYbAYyfeA4/yVrCSseAnC3zUFh5KR2ULlaUQNti6guq0LEZxr0iBYpDA0myrEac/jvZh3lNG+gm2rO7mkbHZwqRZIMBVQ7DjkBIAF5SO6kFZ0fKsSGqkJoI/1AhlGpAbao2wAW2e0lukz51VfbVqtcjjauL8dPCNDwSE+owzF1YdtGLptbUKge8GurUsvcXMKR9JLHMQbAtIPeAuDQ5q2FIi0tkdEgKCJEafTIScg/ciLRByv1ZzV5LQUKBH'
    }
    response = requests.request("POST", url, headers=headers, data=payload)
    print(response.status_code)
    print(response.text)


def get(asin):
    session = requests.Session()
    session.keep_alive = False  # 关闭多余连接
    ip = '122.194.209.150:37854'
    proxy_handler = {
        "http": "socks5h://" + ip,
        "https": "socks5h://" + ip,
    }
    url = "https://www.amazon.com/product-reviews/{}".format(asin)
    print(url)
    # try:
    random_arg = str(random.randint(1111111, 9999999))
    cookie = {
        "session-id": "139-5890879-{}".format(random_arg),
        "session-token": "IDm4MzK2EIfmnA78GamvtZTbJeHGIoI7HtbbNzySKqI5L1cph1hZjeMHS4FyVrsqlPCndF2dxAMckDprztvQc/rlao7x88VpqQnoRpt66JCAJQ/p37UzTD1C/Hag0Itju34jaZjxMJfRM0bLUi8jKt0rKBEN9XJ0t0VxiU9l44WMtZYoOJmiMOE9A62M3KyJS9iUig0M+rUHEoBFZkfK5H0rP8ksA4Lk1ecGwklvPMm+wrTxjKzNxhSbZUBPVbPAepjxvVf8nuU=",
        "ubid-main": "134-8468621-{}".format(random_arg),
        "session-id-time": "2223543209l",
        "i18n-prefs": "USD",
    }
    headers['path'] = url.replace('https://www.amazon.com/', '')
    headers[
        'User-Agent'] = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)Chrome/70.0.3538.77 Safari/537.36'
    headers['downlink'] = random.choice(downlink_list)
    headers['ect'] = '4g'
    # headers['cookie'] = cookie
    headers['rtt'] = str(random.randrange(0, 300, 50))
    r = session.get('https://www.amazon.com/', headers=headers, timeout=20, proxies=proxy_handler, verify=False, cookies=cookie)
    print(r.status_code)
    # print(r.text)
    print(r.cookies)
    cookie = requests.utils.dict_from_cookiejar(r.cookies)
    print(cookie)
    return r.status_code
    # print(r.text)
    # except:
    #     content_html = ''
    #     return content_html

get('B07TTVZM9S')
#
# asin = 'B07TTVZM9S'
# t1 = MyThread(get, (asin,))
# t2 = MyThread(post, (asin,))
# t1.start()
# t2.start()
# t1.join()
# t2.join()
# r = t1.get_result()
# print("r :" + str(r))
# print('666')
